package com.example.eeuse.controller;

import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.example.eeuse.listener.SubsidyMesDataVoListenter;
import com.example.eeuse.mapper.SubsidyRecordDataMapper;
import com.example.eeuse.mapper.BankAccountMapper;
import com.example.eeuse.model.BankAccount;
import com.example.eeuse.model.SubsidyRecordData;
import com.example.eeuse.model.vo.SubsidyRecordDataVo;
import com.example.eeuse.model.vo.BankAccountVo;
import com.example.eeuse.listener.BankAccountVoListenter;
import com.example.eeuse.util.AutoNameUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
 @RequestMapping("/oscdata")
public class OscController {

    @Resource
    private SubsidyRecordDataMapper subsidyRecordDataMapper;
    @Resource
    private BankAccountMapper bankAccountDataMapper;

    @GetMapping("/insert")
    public Integer insert() {
        // 初始化-> 新增数据
        BankAccount yearSubsidyInfo = BankAccount.builder().build();
        return bankAccountDataMapper.insert(yearSubsidyInfo);
    }

    @PostMapping("/search")
    public List<BankAccount> search(String blankName) {
        LambdaEsQueryWrapper<BankAccount> queryWrapper=new LambdaEsQueryWrapper();
            queryWrapper.like(BankAccount::getAccountCode,blankName);
        return     bankAccountDataMapper.selectList(queryWrapper);
    }

    @GetMapping("/importbtjl")
    public String importBtjlData(String filename) {
        String path="E:\\xls\\" + filename ;
        String bentchId = UUID.randomUUID().toString();
        log.info("导入文件{},批次ID {}",path,bentchId);
        EasyExcel.read(path, SubsidyRecordDataVo.class, new SubsidyMesDataVoListenter(subsidyRecordDataMapper)).sheet(0).doRead();
        log.info("批次ID {} 导入完成",bentchId);
        return String.format("批次ID {%s} 导入完成",bentchId);
    }

    @GetMapping("/importbtjlall")
    public String importBtjlAllData(String filename) {
        List<String> bidList = new ArrayList<>();
        for(int i=0;i<5;i++) {
            String path = "E:\\xls\\0" + (i + 1) + ".xlsx";
            String bentchId = UUID.randomUUID().toString();
            log.info("导入文件{},批次ID {}", path, bentchId);
            EasyExcel.read(path, SubsidyRecordDataVo.class, new SubsidyMesDataVoListenter(subsidyRecordDataMapper)).sheet(0).doRead();
            log.info("批次ID {} 导入完成", bentchId);

            bidList.add(String.format("批次ID {%s} 导入完成",bentchId));
        }
        return String.join(",",bidList);
    }

    @GetMapping("/autobtjl")
    public String autoBtjlData(Integer cnt) {
        List<SubsidyRecordData> list=new ArrayList<>();
        String code= RandomUtil.randomNumbers(18);
        String bentchId = IdUtil.fastUUID();

        int BATCH_COUNT = 30000;
        int quotient = cnt / BATCH_COUNT;
        int remainder = cnt % BATCH_COUNT;

        for(int m=0;m<quotient;m++) {
            list = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
            for (int i = 0; i < BATCH_COUNT; i++) {
                String name = AutoNameUtil.autoName();
                SubsidyRecordData build = SubsidyRecordData.builder()
                        .id(IdUtil.fastUUID())
                        .collectId(IdUtil.fastUUID())
                        .peopleId(RandomUtil.randomNumbers(19))
                        .peopleVersion("2")
                        .accountVersoin("2")
                        .projectId(RandomUtil.randomString(1).toUpperCase() + "-" + RandomUtil.randomString(2).toUpperCase() + RandomUtil.randomNumbers(3))
                        .regionId(RandomUtil.randomString(16))
                        .yearId(2022)
                        .batchId(bentchId)
                        .money(RandomUtil.randomLong(65532, 865454) / 20F)
                        .operatorId("410211013")
                        .operatorTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                        .status("6")
                        .assingTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                        .payTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                        .assingId(IdUtil.fastUUID())
                        .bankType("02")
                        .accountCode(RandomUtil.randomNumbers(18))
                        .accountName(name)
                        .name(name)
                        .createTime(new Date())
                        .build();
                list.add(build);
            }
            Integer integer = subsidyRecordDataMapper.insertBatch(list);
            log.info("生成补贴记录完成{},数量为：{}", code, integer);
        }

        list = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        for (int i = 0; i < remainder; i++) {
            String name = AutoNameUtil.autoName();
            SubsidyRecordData build = SubsidyRecordData.builder()
                    .id(IdUtil.fastUUID())
                    .collectId(IdUtil.fastUUID())
                    .peopleId(RandomUtil.randomNumbers(19))
                    .peopleVersion("2")
                    .accountVersoin("2")
                    .projectId(RandomUtil.randomString(1).toUpperCase() + "-" + RandomUtil.randomString(2).toUpperCase() + RandomUtil.randomNumbers(3))
                    .regionId(RandomUtil.randomString(16))
                    .yearId(2022)
                    .batchId(bentchId)
                    .money(RandomUtil.randomLong(65532, 865454) / 20F)
                    .operatorId("410211013")
                    .operatorTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                    .status("6")
                    .assingTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                    .payTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                    .assingId(IdUtil.fastUUID())
                    .bankType("02")
                    .accountCode(RandomUtil.randomNumbers(18))
                    .accountName(name)
                    .name(name)
                    .createTime(new Date())
                    .build();
            list.add(build);
        }
        Integer integer = subsidyRecordDataMapper.insertBatch(list);
        log.info("生成补贴记录完成{},数量为：{}", code, integer);

        return String.format("批次ID {} 导入完成",bentchId);
    }

    @GetMapping("/importyhzh")
    public String importYhzhData(String filename) {
        String path="E:\\xls\\" + filename;
        String bentchId = UUID.randomUUID().toString();
        log.info("导入文件{},批次ID {}",path,bentchId);
        EasyExcel.read(path, BankAccountVo.class, new BankAccountVoListenter(bankAccountDataMapper)).sheet(0).doRead();
        log.info("批次ID {} 导入完成",bentchId);
        return String.format("批次ID {%s} 导入完成",bentchId);
    }



    @GetMapping("/importyhzhall")
    public String importYhzhAllData() {
        List<String> bidList = new ArrayList<>();
        for(int i=0;i<5;i++){
            String path="E:\\xls\\yhzh0" + (i+1)+ ".xlsx";
            String bentchId = UUID.randomUUID().toString();
            log.info("导入文件{},批次ID {}",path,bentchId);
            EasyExcel.read(path, BankAccountVo.class, new BankAccountVoListenter(bankAccountDataMapper)).sheet(0).doRead();
            log.info("批次ID {} 导入完成",bentchId);

            bidList.add(String.format("批次ID {%s} 导入完成",bentchId));
        }

        return String.join(",",bidList);
    }


    @GetMapping("/autoyhzh")
    public String autoYhzhData(Integer cnt) {
        int BATCH_COUNT = 30000;
        int quotient = cnt / BATCH_COUNT;
        int remainder = cnt % BATCH_COUNT;

        List<BankAccount> list=new ArrayList<>();
        String code= RandomUtil.randomNumbers(18);
        for(int m=0;m<quotient;m++){
            list = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
            for (int i = 0; i <BATCH_COUNT ; i++) {
                String name = AutoNameUtil.autoName();
                BankAccount build = BankAccount.builder()
                        .id(IdUtil.fastUUID())
                        .peopleId(RandomUtil.randomNumbers(19))
                        .bankType("02")
                        .accountCode(RandomUtil.randomNumbers(18))
                        .accountName(name)
                        .name(name)
                        .createTime(new Date())
                        .build();
                list.add(build);
            }
            Integer integer = bankAccountDataMapper.insertBatch(list);
            log.info("自动生成完成{},生成数量为：{}",code,integer);
        }

        list = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        for (int i = 0; i <remainder ; i++) {
            String name = AutoNameUtil.autoName();
            BankAccount build = BankAccount.builder()
                    .id(IdUtil.fastUUID())
                    .peopleId(RandomUtil.randomNumbers(19))
                    .bankType("02")
                    .accountCode(RandomUtil.randomNumbers(18))
                    .accountName(name)
                    .name(name)
                    .createTime(new Date())
                    .build();
            list.add(build);
        }
        Integer integer = bankAccountDataMapper.insertBatch(list);
        log.info("自动生成完成{},生成数量为：{}",code,integer);

        return String.format("自动生成完成 {%d} 条",integer);
    }
}
